package game.module.mail.dao;

import game.db.DataSourceManager;
import game.module.mail.bean.HallMailBean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: Pomelo.Lee
 * @date: 2017年8月24日 下午6:45:42
 */
public class MailDao {

	private static Logger logger = LoggerFactory.getLogger(MailDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static MailDao instance = new MailDao();
	}

	public static MailDao getInstance() {
		return SingletonHolder.instance;
	}

	private ResultSetHandler<List<HallMailBean>> mutiMailHandler = new ResultSetHandler<List<HallMailBean>>() {

		@Override
		public List<HallMailBean> handle(ResultSet rs) throws SQLException {
			List<HallMailBean> mailBeanList = new ArrayList<HallMailBean>();
			while (rs.next()) {
				HallMailBean mailBean = new HallMailBean();
				mailBean.setId(rs.getLong("id"));
				mailBean.setPlayerId(rs.getInt("player_id"));
				mailBean.setTemplateId(rs.getInt("template_id"));
				mailBean.setSendPlayerId(rs.getInt("send_player_id"));
				mailBean.setSendName(rs.getString("send_name"));
				mailBean.setMailTitle(rs.getString("mail_title"));
				mailBean.setMailContent(rs.getString("mail_content"));
				mailBean.setMailAttach(rs.getString("mail_attach"));
				mailBean.setCreateTime(rs.getTimestamp("create_time"));
				mailBean.setIsRead(rs.getInt("is_read"));
				mailBean.setDelFlag(rs.getInt("del_flag"));
				mailBean.setOtherMsg(rs.getString("other_msg"));

				mailBeanList.add(mailBean);
			}
			return mailBeanList;
		}
	};

	public boolean addUserMail(HallMailBean mailBean) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			int ret = runner.update(
					"insert into mail(id,player_id,template_id,send_player_id,send_name,mail_title,mail_content,mail_attach,create_time,is_read,del_flag,other_msg) values(?,?,?,?,?,?,?,?,NOW(),?,?,?)",
					mailBean.getId(), mailBean.getPlayerId(), mailBean.getTemplateId(), mailBean.getSendPlayerId(), mailBean.getSendName(), mailBean.getMailTitle(),
					mailBean.getMailContent(), mailBean.getMailAttach(), 0, 0, mailBean.getOtherMsg());
			if (ret > 0) {
				addRet = true;
			}
		} catch (SQLException e) {
			logger.error("", e);
		}
		return addRet;
	}

	public List<HallMailBean> getMailByPlayerId(int playerId) {
		List<HallMailBean> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from mail where player_id = ? and del_flag=0  order by id desc", mutiMailHandler, playerId);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public List<HallMailBean> getValidMailByPlayerId(int playerId, int lastMailId) {
		List<HallMailBean> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from mail where player_id = ? and del_flag=0 and id>?", mutiMailHandler, playerId, lastMailId);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public void updateMailState(long id, int isRead, boolean isUpdateTime) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			String updateSql = "update mail set is_read = ? where id=?";
			if (isUpdateTime) {
				updateSql = "update mail set is_read = ? ,gain_time=NOW() where id=?";
			}
			runner.update(updateSql, isRead, id);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

	public void removeMail(long id) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("update  mail set del_flag=1 where id = ? ", id);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

	public void delPastDueMail(Date date) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("DELETE FROM mail WHERE create_time < ?", date);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

}
